import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-local';
import { CustomException, ErrorCode } from '../exceptions/custom.exception';
import { UserService } from 'src/module/user/user.service';
/**
 * local 策略
 */
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
  constructor(private readonly userService: UserService) {
    super();
  }
  /**
   * 会自己调用 validate 方法 验证成功后会在请求头中注入 user
   * @param username
   * @param password
   * @returns
   */
  async validate(username: string, password: string) {
    const user = await this.userService.validateUser(username, password);
    if (!user) throw new CustomException(ErrorCode.ERR_10002);
    return user;
  }
}
